#import "PlayScreen.h"
#import "State.h"
#import "CharInfo.h"
#import <QuartzCore/QuartzCore.h>
@implementation PlayScreen

@synthesize stage,player,enemy;
- (PlayScreen*)initWithFrame:(CGRect)frame withPlayer:(CharInfo*)p at:(int)currentStage {
	if ([super initWithFrame:frame]){
		commandUnknown = [UIImage imageNamed:@"command_unknown.png"];
		commandRock = [UIImage imageNamed:@"command_rock.png"];
		commandScissor = [UIImage imageNamed:@"command_scissor.png"];
		commandPaper = [UIImage imageNamed:@"command_paper.png"];
		hud = [UIImage imageNamed:@"top.png"];
		
        score1=[[UILabel alloc] initWithFrame:CGRectMake(40, 2, 130, 16)];
        score1.text=@"0000000000";
		score1.bounds=CGRectMake(40, 0, 200, 20);
		
        name=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
		name.bounds=CGRectMake(0, 40, 200, 20);
        name.text=p.name;
		
        name2=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
		name2.bounds=CGRectMake(220, 40, 100, 20);
		name2.textAlignment=UITextAlignmentRight;
		
		text = [[UILabel alloc] initWithFrame:CGRectMake(0,0,240,240)];
		text.font=[UIFont systemFontOfSize:60];
		text.textAlignment=UITextAlignmentCenter;
		text.bounds=CGRectMake(40, 120,240, 240);
		text.textColor=[UIColor colorWithWhite:1 alpha:1];
		
		time = [[UILabel alloc] initWithFrame:CGRectMake(0,0,60,50)];
		time.font=[UIFont systemFontOfSize:35];
		time.textAlignment=UITextAlignmentCenter;
		time.bounds=CGRectMake(130, 25,60, 50);
		
        time.text=@"200";
		counter = 0;
		state = G_START;
		mainState=S_PLAY;
		lastResult=R_UKNOWN;
		endTime = CACurrentMediaTime();
		p.health=100;
        [self initPlayer:p];
		stageNr=currentStage;
        [self initStage:currentStage];
		enemy = [[Enemy alloc] init:stage.enemy];
		name2.text=enemy.info.name;

		self.image = stage.image;
		score=100;
		text.text=@"";
	}
    return self;
}


- (void)  action:(int)x y:(int)y{
		if( state == G_THREE||state == G_TWEE|| state == G_ONE)
			[player setCommand:(x / 100)];
	}

	 
	 - (int)  stateMachine {
         
		 double x =CACurrentMediaTime();
		 x=x-endTime;
		 //x=x/1000;
		 x=200-x;
		 if (x < 0) x = 0;
		 time.text= [NSString stringWithFormat:@"%03d", (int)x];
		if (counter < 4) {
			counter++;
			return mainState;
		}
		counter=0;
		switch (state) {
		case G_START:
			[self onStart];
			break;
		case G_THREE:
			[self onThree];
			break;
		case G_TWEE:
			[self onTwo];
			break;
		case G_ONE:
			[self onOne];
			break;
		case G_FIGHT:
			[self onFight];
			break;
		case G_DECIDE:
			[self onDecide];
			break;
		case G_CELEBRATE:
			[self onCelebrate];
			break;
		case G_FINISH:
			[self onFinish];
			break;
		default:
			[self onStart];
			break;
		}
		return mainState;
	}

	 - (void)  onFinish {
		 if( player.info.health>enemy.info.health){
			 player.info.state=STATE_LEVELING;
			 enemy.info.state=STATE_DYING;
			 text.text = [NSString stringWithFormat:@"Stage %d cleared",stageNr ];
			 if(stageNr<6)
				 state=mainState= S_TRANSITION;
			 else
				 state=mainState= S_END;
		}else{
			player.info.state=STATE_DYING;
			enemy.info.state=STATE_LEVELING;
			text.text= @"You lose!";
			state=mainState= S_GAMEOVER;
		}

	}

	 - (void)  onCelebrate {
		if (lastResult == R_LOSING) {// lose
			player.info.state=STATE_LOSSING;
			enemy.info.state=STATE_WINNING;
			text.text= @"";
		} else if (lastResult == R_WINNING) {// win
			player.info.state=STATE_WINNING;
			enemy.info.state=STATE_LOSSING;
			player.info.score+=score;
			text.text= @"";

		} else {
			player.info.state=STATE_DRAWING;
			enemy.info.state=STATE_DRAWING;
			text.text= @"";
		}
		lastResult=R_UKNOWN;
         [player setCommand:-1 ];
         [enemy setCommand:-1 ];
		if (player.info.health > 0 && enemy.info.health > 0 )//&&(System currentTimeMillis<endTime) )
			state=G_THREE;
		else
			state= G_FINISH;
	}

	 - (void)  onDecide {
		if(lastResult==R_UKNOWN)
			lastResult = [self decide:player.info.command eC:enemy.info.command];
			switch (lastResult) {
			case 1://player win
				text.text= @"Win";
				if(player.info.magic<100)
					player.info.magic+=7;
				if (enemy.info.health > 0)
					enemy.info.health-=10;
				break;
			case -1://player lose
				text.text= @"Lose";
				if(enemy.info.magic<100)
					enemy.info.magic+=7;
				if (player.info.health > 0)
					player.info.health-=10;
				break;
			default:
				text.text= @"Draw";
                    player.info.magic=3;
                    enemy.info.magic=3;
				break;
			}
		state=G_CELEBRATE;
	}

	 - (void)  onFight {
		player.info.state=STATE_SHOWING;
		enemy.info.state=STATE_SHOWING;
		state= G_DECIDE;
	}

	 - (void)  onOne {
		text.text= @"1";
		if(enemy.info.command== COMMAND_UNKNOWN) 
			[enemy generateCommand];

		state= G_FIGHT;
	}

	 - (void)  onTwo {
		player.info.state=STATE_IDLE;
		enemy.info.state=STATE_IDLE;
		text.text= @"2";
		state= G_ONE;
	}

	 - (void)  onThree {
		player.info.state=STATE_NONE;
		enemy.info.state=STATE_NONE;
		text.text= @"3";
		state= G_TWEE;
	}

	 - (void)  onStart {
		player.info.state=STATE_NONE;
		enemy.info.state=STATE_NONE;
		 text.text= [NSString stringWithFormat:@"Stage %d start",stageNr];
		state= G_THREE;
	}
	 - (void)  drawTime{

	}

-(int)decide:(int)playerCommand eC:(int)enemyCommand {
		int res = enemyCommand - playerCommand;
		if (playerCommand <0){
			return R_LOSING;
		}
		if ( res == -1 || res == 2) {// draw
			score=100;
			return R_LOSING;
		} else if (res == 1 || res == -2) {// win
			score+=100;
			return R_WINNING;
		} else {
			if(score>100)score-=50;
			return R_DRAWING;
		}
	}


	 - (void)  initStage:(int)currentStage {
		 stage= [[Stage alloc]init:currentStage];
	}

	 
	 - (void)  paintScreen{
       //[super drawRect:CGRectMake(0, 0, 320, 480)];
		 [stage.image drawAtPoint:CGPointMake(0.0f,0.0f)];
		[self drawPlayers];
		[self drawHud];
		[self drawTime];
		[self drawText];
		[self drawCommands];
	}
	 - (void)  drawHud{
         
        [hud drawAtPoint:CGPointMake(0.0f,0.0f)];
		 [player.icon drawRect:CGRectMake(2,   2, 30, 30)];
		 [enemy.icon drawRect:CGRectMake( 288, 2, 30, 30)];
		CGContextRef context=UIGraphicsGetCurrentContext();

		 CGContextSetRGBFillColor(context, 1, 0, 0, 1); // darker blue (bottom)
		 CGContextSetRGBStrokeColor(context,0,0,0,1); // darker blue (bottom)
		 CGContextSetLineWidth(context,2);

	
		 CGRect pLine=CGRectMake(30, 20, 100, 20);
		 CGRect eLine=CGRectMake(190, 20, 100, 20);
		 CGContextFillRect(context, pLine);
		 CGContextFillRect(context,eLine);
         //lPa(int)  setShadowLayer(3, 2, 2, 0xFF000000);
		 CGContextStrokeRect(context, pLine);
		 CGContextStrokeRect(context,eLine);
         
		 CGContextSetRGBFillColor(context, 1, 1, 0, 1);
		 pLine.size.width= player.info.health;
		 CGContextFillRect(context, pLine);
		 eLine.origin.x = 290 - enemy.info.health;
		 eLine.size.width = enemy.info.health;
		 
		 CGContextFillRect(context, eLine);

		
	
		 CGContextSetRGBFillColor(context, 1, 1, 1, 1);
  		CGContextSetFontSize(context, 16);
         [score1 setText: [NSString stringWithFormat:@"%07d", player.info.score]];
         [score1 drawRect: score1.frame];
		 [name drawRect: name.frame];
		 [name2 drawRect: name.frame];
		 [time drawRect: time.frame];
          
		//CGContextSetTextDrawingMode(context, CGCon)setTextAlign(Align LEFT);
		//CGContext drawText(player.info name, 32, 51, tPa(int) );
		//g drawText(Position SCORE + player.info score, Position SPAN+ Position ICON_SIZE, Position SCOREY, tPa(int) );
		//tPa(int)  setTextAlign(Align RIGHT);
		//g drawText(enemy.info name, 248, 51, tPa(int) );

		//tPa(int)  setTextAlign(Align CENTER);
		//CGStirng s=@"Time"
         
        // , 160, Position TIMEY, tPa(int) );
	}

	 - (void)  drawPlayers {
		[player paint];
		[enemy paint];
	}
	 - (void)  drawCommands{
         int i=player.info.command;
		switch(i){
		case  COMMAND_UNKNOWN:
				[commandUnknown drawAtPoint:CGPointMake( 0, 400) ];
			break;
		case  COMMAND_ROCK:
				[commandRock drawAtPoint:CGPointMake( 0, 400)];
				break;
		case  COMMAND_SCISSOR:
				[commandScissor drawAtPoint:CGPointMake(  0, 400)];
				break;
		case  COMMAND_PAPER:
				[commandPaper drawAtPoint:CGPointMake(  0, 400)];
				 break;
		}

	}
	 - (void)  drawText{
		 [text drawRect: time.frame];

	}
- (void)  initEnemy:(CharInfo*) e {
		enemy =[[Enemy alloc]init:e];
	}
- (void)  initPlayer:(CharInfo*) p {
    player = [[Player alloc]init:p];

	}
@end
